<?php
    class Admincp_PageController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        private $_locale;
        
        public function init()
        {
            $this->loadTemplates('admin');
            $this->view->headTitle('Quản lý trang');
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Admincp_Model_Page();
            $this->_locale = 'vi';
        }
        
        public function preDispatch()
		{
		    $auth = Zend_Auth::getInstance();
		    if(!$auth->hasIdentity()) {
		        if($this->_request->getActionName() != 'login')
		            $this->_redirect('admincp/index/login');
		    }
		}
        
        public function indexAction()
        {
            if($_POST['add']) {
                $this->_redirect('admincp/page/create');
            }
            if($_POST['delete']) {
                $listId = $this->_arrParam['check'];
                $this->_model->deleteMultiPage($listId);
                $this->_redirect($_SERVER['HTTP_REFERER']);
            }
            
            $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->listPage($this->_locale));
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(40);
            $paginator->setPageRange(5);
            $currentPage = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data = $paginator;
            $this->view->total = $paginator->getTotalItemCount();
        }
        
        public function createAction()
        {
            $language = new Admincp_Model_Language();
            $this->view->language = $language->listLanguageFull();
            
            if($this->_request->isPost()) {
                $strTrim = new Zend_Filter_StringTrim();
                $title['vi'] = $strTrim->filter(strip_tags($this->_arrParam['title-vi']));
                $title['zh'] = $strTrim->filter(strip_tags($this->_arrParam['title-zh']));
                $title['en'] = $strTrim->filter(strip_tags($this->_arrParam['title-en']));
                $title['trv'] = $strTrim->filter(strip_tags($this->_arrParam['title-trv']));
                $description['vi'] = $strTrim->filter(stripslashes($this->_arrParam['description-vi']));
                $description['zh'] = $strTrim->filter(stripslashes($this->_arrParam['description-zh']));
                $description['en'] = $strTrim->filter(stripslashes($this->_arrParam['description-en']));
                $description['trv'] = $strTrim->filter(stripslashes($this->_arrParam['description-trv']));
                $data_page = array(
                    'status'=>1,
                );
                $page_id = $this->_model->addPage($data_page);
                foreach($title as $key=>$value) {
                    foreach($this->view->language as $lang) {
                        if($key == $lang['locale']) {
                            $data = array(
                                'page_id'=>$page_id,
                                'language_id'=>$lang['id'],
                                'title'=>$value,
                                'description'=>$description[$key],
                            );
                            $this->_model->addPageLanguage($data);
                            break;
                        }
                    }
                }
                $this->_redirect('admincp/page');
            }
        }
        
        public function editAction()
        {
            $id = (int) $this->_arrParam['id'];
            $this->view->data = $this->_model->getPageById($id);
            $this->view->id = $id;
            $language = new Admincp_Model_Language();
            $this->view->language = $language->listLanguageFull();
            
            if($this->_request->isPost()) {
                $strTrim = new Zend_Filter_StringTrim();
                $title['vi'] = $strTrim->filter(strip_tags($this->_arrParam['title-vi']));
                $title['zh'] = $strTrim->filter(strip_tags($this->_arrParam['title-zh']));
                $title['en'] = $strTrim->filter(strip_tags($this->_arrParam['title-en']));
                $title['trv'] = $strTrim->filter(strip_tags($this->_arrParam['title-trv']));
                $description['vi'] = $strTrim->filter(stripslashes($this->_arrParam['description-vi']));
                $description['zh'] = $strTrim->filter(stripslashes($this->_arrParam['description-zh']));
                $description['en'] = $strTrim->filter(stripslashes($this->_arrParam['description-en']));
                $description['trv'] = $strTrim->filter(stripslashes($this->_arrParam['description-trv']));
                
                foreach($title as $key=>$value) {
                    foreach($this->view->language as $lang) {
                        if($key == $lang['locale']) {
                            $data = array(
                                'language_id'=>$lang['id'],
                                'title'=>$value,
                                'description'=>$description[$key],
                            );
                            $this->_model->updatePageLanguage($data, $id);
                            break;
                        }
                    }
                }
                $this->_redirect('admincp/page');
            }
        }
        
        public function deleteAction()
        {
            $id = (int) $this->_arrParam['id'];
            $this->_model->deletePage($id);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function publishAction()
        {
            $id = (int) $this->_arrParam['id'];
            $status = (int) $this->_arrParam['status'];
            $status = ($status == 1) ? 2 : 1;
            $data = array(
                'status'=>$status,
            );
            $this->_model->updatePage($data, $id);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function checkAction()
        {
            $this->_helper->layout()->disableLayout();
            $id = (int) $this->_arrParam['id'];
            $title = $this->_arrParam['title'];
            if($this->_model->checkPage($title, $id) > 0) {
                echo 'Tiêu đề trang đã tồn tại';
            }
            else {
                echo '';
            }
        }
    }
